<?php
namespace Admin\Controller;
use Libs\Logs;
class ArticleController extends AdminController {

    function _initialize() {
        parent::_initialize();
        $this->assign('sys', 'news'); // 数据集
    }
    
    //显示文章内容
    public function index(){
        $article = M('article');
        //若有查询执行此处下
        $where = NULL;
        if(I('get.title')){$a = '%'.I('get.title').'%'; $where['title'] = array('like',$a);} //得到要查询的题目
        if(I('get.cat_id')){$where['cat_id'] = array('eq',I('get.cat_id'));} //得到要查询的类型
        //若有查询执行此处上
        $count = $article->where($where)->order('article_id desc')->count();//获得满足条件的记录数
        $Page  = new \Think\Page($count,15);//实例化分页类 传入总记录数和每页显示的记录数(10)
        $show  = $Page->show();// 分页显示输出
        $list = $article->where($where)->order('article_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 数据集
        $this->assign('page',$show);// 页码
        $cat = M('article_cat')->where('show_in_nav = 1')->select();
        $this->assign('cat',$cat);
        $this->assign('title',I('get.title'));// 搜索的题目
        $this->assign('id',I('get.cat_id'));// 搜索的分类
        $this->assign('action', 'article');
        $this->display();//输出到模板
    }


    //添加 文章显示
    public function add(){
        //I('get.article_id')若存在则为 编辑，相反则为 添加
        if(IS_POST){
            //  编辑模式
            $article = M('article');
            $data['cat_id'] = I('cat_id');
            $data['title'] = I('title');
            $data['content'] = cJscript($_POST['content']);
            $data['author'] = I('author');
            $data['keywords'] = I('keywords');
            $data['description'] = I('description');
            $data['article_type'] = I('article_type');
            $data['is_open'] = I('is_open');
            $data['add_time'] = NOW_TIME;
            $data['file_url'] = I('file_url');
            $data['link'] = I('link');
            $data['browse'] = I('browse');
            $re = $article->add($data);
            if($re){
                    //日志记录
                    $logobj = new Logs();
                    $html = I('title');
                    $logobj->write(array("管理员:" . session('manager_auth.truename'), "新闻添加", $html));
                    
                $this->success('添加成功！',U('index'));
            }else{
                    //日志记录
                    $logobj = new Logs();
                    $html = I('title');
                    $logobj->write(array("管理员:" . session('manager_auth.truename'), "新闻添加失败！", $html));
                
                $this->error('添加失败');
            }
        }
        
        $this->assign('action', 'article');
        $cat = M('article_cat')->where('show_in_nav = 1')->select();
        $this->assign('cat',$cat);// 分类数据
        $this->display();//输出到模板
    }
    
    //编辑 文章显示
    public function edit(){
        //I('get.article_id')若存在则为 编辑，相反则为 添加
        $article_id = I('id');
        $aModel = M('article');
        if(IS_POST){
            //  编辑模式
            $data['cat_id'] = I('cat_id');
            $data['title'] = I('title');
            $data['content'] = cJscript($_POST['content']);
            if(I('author')){
                $data['author'] = I('author');
            }
            if(I('keywords')){
                $data['keywords'] = I('keywords');
            }
            if(I('description')){
                $data['description'] = I('description');
            }
            $data['article_type'] = I('article_type');
            $data['is_open'] = I('is_open');
            $data['add_time'] = NOW_TIME;
            if(I('file_url')){
                $data['file_url'] = I('file_url');
            }
            if(I('link')){
                $data['link'] = I('link');
            }
            $re = $aModel->where(array('article_id'=>$article_id))->data($data)->save();
            if($re !== FALSE){
                    //日志记录
                    $logobj = new Logs();
                    $html = I('title');
                    $logobj->write(array("管理员:" . session('manager_auth.truename'), "新闻修改", $html));
                    
                $this->success('修改成功！',U('index'));
            }else{
                    //日志记录
                    $logobj = new Logs();
                    $html = I('title');
                    $logobj->write(array("管理员:" . session('manager_auth.truename'), "新闻修改失败！", $html));
                
                $this->error('修改失败');
            }
        }

        $article = $aModel->where(array('article_id'=>$article_id))->find();
        $this->assign('ar', $article);
        $this->assign('action', 'article');
        $cat = M('article_cat')->where('show_in_nav = 1')->select();
        $this->assign('cat',$cat);// 分类数据
        $this->assign('article_id',$article_id);
        $this->assign('action', 'article');
        $this->display();//输出到模板
    }
    
    //执行删除
    public function delete(){
        $article = M('article');
        $Id = I('post.data', 0, 'intval');
        if (empty($Id)) {
            $this->error('参数不能为空！');
        }
        $article_id = 'article_id='.$Id;
        $if = $article->where($article_id)->delete();
        if ($if) {
            //日志记录
            $logobj = new Logs();
            $html = '删除文章ID'.$Id;
            $logobj->write(array("管理员:" . session('manager_auth.truename'), "删除文章", $html));
            
            $this->success('删除成功', '/Article/index');
        }else{
            $this->error('删除失败','/Article/index');
        }

    }

}